import {useRolesStore} from "@/store/rolesStroe.ts";
import {ElMessageBox, ElNotification} from "element-plus";
import {formatDateTime} from "@/utils/time.ts";
import {deleteRole, deleteRoles} from "@/api/role";

export default function () {
    const rolesStore = useRolesStore()

    const total = ref(0)
    const selectRowID = ref<number[]>([])
    const search = async () => {
        rolesStore.reqData.name = rolesStore.filteredData.name
        rolesStore.reqData.isUsable = rolesStore.filteredData.isUsable
        try {
            total.value = await rolesStore.getRoleListFun()
        } catch (e) {
            ElNotification({
                message: e as string,
                type: 'error',
            })
        }
    }
    const handleSizeChange = async (val: number) => {
        rolesStore.reqData.pageSize = val
        try {
            total.value = await rolesStore.getRoleListFun()
        } catch (e) {
            ElNotification({
                message: e as string,
                type: 'error',
            })
        }
    }
    const handleCurrentChange = async (val: number) => {
        rolesStore.reqData.currentPage = val
        try {
            total.value = await rolesStore.getRoleListFun()
        } catch (e) {
            ElNotification({
                message: e as string,
                type: 'error',
            })
        }
    }
    onBeforeMount(async () => {
        try {
            total.value = await rolesStore.getRoleListFun()
        } catch (e) {
        }
    })
    const formatCATTime = (row: any) => {
        return formatDateTime(row.CreatedAt)
    }
    const formatUPDTime = (row: any): string => {
        if (row.UpdatedAt == '0001-01-01T00:00:00Z') {
            return ''
        }
        return formatDateTime(row.UpdatedAt)
    }
    const add = () => {
        rolesStore.openAdd = true
    }
    const edit = (row: any) => {
        rolesStore.openEdit = true
        rolesStore.roleId = row.ID
    }
    const del = async (row: any) => {
        ElMessageBox.confirm("确定删除角色" + row.name + "吗？", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(async () => {
            const res = await deleteRole(row.ID)
            if (res.code == 200) {
                ElNotification({
                    message: '删除成功',
                    type: 'success'
                })
                await rolesStore.getRoleListFun()
            } else {
                ElNotification({
                    message: '删除失败',
                    type: 'error'
                })
            }
        }).catch(() => {
        })
    }
    const selected = (row: any) => {
        let rowId: number[] = []
        row.forEach((item: any) => {
            rowId.push(item.ID)
        })
        selectRowID.value = rowId

    }
    const selectedall = (row: any) => {
        let rowId: number[] = []
        row.forEach((item: any) => {
            rowId.push(item.ID)
        })
        selectRowID.value = rowId

    }
    const handleBatchDelete = async () => {
        if (selectRowID.value.length == 0) {
            ElNotification({
                message: '请选择要删除的数据',
                type: 'warning'
            })
            return false
        }
        ElMessageBox.confirm("确定永久删除这" + selectRowID.value.length + "条角色数据吗？", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(async () => {
            const res = await deleteRoles(selectRowID.value)
            if (res.code == 200) {
                ElNotification({
                    message: '批量删除成功',
                    type: 'success'
                })
                await rolesStore.getRoleListFun()
            } else {
                ElNotification({
                    message: '批量删除失败',
                    type: 'error'
                })
            }
        }).catch(() => {

        })
    }
    return {
        rolesStore,
        search,
        formatCATTime,
        formatUPDTime,
        add,
        edit,
        del,
        handleSizeChange,
        handleCurrentChange,
        total,
        selected,
        selectedall,
        handleBatchDelete
    }
}